﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessClasses;

namespace TicketsApplication
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            dataAccessLayer = new DataAccessClasses.DataAccessLayer();
            categoriesDataGridView.DataSource = dataAccessLayer.Categories;
            locationsDataGridView.DataSource = dataAccessLayer.Locations;
            categoryBindingSource.DataSource = dataAccessLayer.Categories;
            locationBindingSource.DataSource = dataAccessLayer.Locations;
            eventsDataGridView.DataSource = dataAccessLayer.Events;
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            dataAccessLayer.SaveChanges();
        }

        private void categoriesCancelButton_Click(object sender, EventArgs e)
        {
            dataAccessLayer.GlobalContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, dataAccessLayer.Categories);
            categoriesDataGridView.DataSource = null;
            categoriesDataGridView.DataSource = dataAccessLayer.Categories;
        }

        private void deleteCategoriesButton_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("Are you sure want to delete selected records?", "Question",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            HashSet<int> indices = new HashSet<int>();

            foreach (DataGridViewCell col in categoriesDataGridView.SelectedCells)
            {

                if (!indices.Contains(col.RowIndex))
                    indices.Add(col.RowIndex);
            }

            foreach (int rowNumber in indices)
            {
                try
                {

                    dataAccessLayer.DeleteCategory(int.Parse(categoriesDataGridView.Rows[rowNumber].Cells[0].Value.ToString()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot delete record in row " + rowNumber + " : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            categoriesDataGridView.DataSource = null;
            categoriesDataGridView.DataSource = dataAccessLayer.Categories;
        }

        private void dataGridView_SizeChanged(object sender, EventArgs e)
        {
            (sender as DataGridView).Size = new Size((sender as DataGridView).Size.Width, tabControl.Size.Height - 70);
        }

        private void locationsCancelButton_Click(object sender, EventArgs e)
        {
            dataAccessLayer.GlobalContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, dataAccessLayer.Locations);
            locationsDataGridView.DataSource = null;
            locationsDataGridView.DataSource = dataAccessLayer.Locations;
        }

        private void locationsDeleteButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure want to delete selected records?", "Question",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            HashSet<int> indices = new HashSet<int>();

            foreach (DataGridViewCell col in locationsDataGridView.SelectedCells)
            {

                if (!indices.Contains(col.RowIndex))
                    indices.Add(col.RowIndex);
            }

            foreach (int rowNumber in indices)
            {
                try
                {
                    dataAccessLayer.DeleteLocation(int.Parse(locationsDataGridView.Rows[rowNumber].Cells[0].Value.ToString()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot delete record in row " + rowNumber + " : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            locationsDataGridView.DataSource = null;
            locationsDataGridView.DataSource = dataAccessLayer.Locations;
        }




        private void cancelEventsButton_Click(object sender, EventArgs e)
        {
            dataAccessLayer.GlobalContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, dataAccessLayer.Events);
            eventsDataGridView.DataSource = null;
            eventsDataGridView.DataSource = dataAccessLayer.Events;
        }

        private void deleteEventButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure want to delete selected records?", "Question",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            HashSet<int> indices = new HashSet<int>();

            foreach (DataGridViewCell col in eventsDataGridView.SelectedCells)
            {

                if (!indices.Contains(col.RowIndex))
                    indices.Add(col.RowIndex);
            }

            foreach (int rowNumber in indices)
            {
                try
                {
                    dataAccessLayer.DeleteEvent(int.Parse(eventsDataGridView.Rows[rowNumber].Cells[0].Value.ToString()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot delete record in row " + rowNumber + " : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            eventsDataGridView.DataSource = null;
            eventsDataGridView.DataSource = dataAccessLayer.Events;
        }

        private void eventsDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            
            if (e.ColumnIndex == 5)
            {
                eventsDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
                MessageBox.Show("The value entered is not a number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
            // MessageBox.Show(e.ColumnIndex.ToString());
        }

 
 

        private void eventsDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            try
            {

                if (e.ColumnIndex == 4)
                {
                    DateTimePickerForm picker = new DateTimePickerForm();
                    DateTime outDate;
                    if (DateTime.TryParse(eventsDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), out outDate))
                    {
                        try
                        {
                            picker.dateTimePicker.Text = eventsDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                        }
                        catch
                        {
                            picker.dateTimePicker.Text = "";
                        }

                    }
                    picker.ShowDialog();
                    eventsDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = picker.dateTimePicker.Text;
                }
            }
            catch
            {

            }
        
        }

    }
}
